﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;

namespace Projektet.Model
{
    class AiSpawnLogic
    {
        private List<CombatUnit> _humanPlayerUnitList;

        public AiSpawnLogic(List<CombatUnit> humanPlayerUnitList) 
        {
            _humanPlayerUnitList = humanPlayerUnitList;
        }

        public List<CombatUnit> GenerateSuggestedSpawnList(Vector2 playerPosition, UnitStats unitStats) 
        {
            List<CombatUnit> spawnSuggestion = new List<CombatUnit>();
            List<CombatUnit> shuffledList = ShuffleList(_humanPlayerUnitList);

            foreach (CombatUnit cU in shuffledList)
            {
                switch (cU.GetType().Name)
                {
                    case "Pikeman":
                        spawnSuggestion.Add(new Archer(PlayerType.Npc, playerPosition, unitStats));
                        break;
                    case "Knight":
                        spawnSuggestion.Add(new Pikeman(PlayerType.Npc, playerPosition, unitStats));
                        break;
                    case "Archer":
                        spawnSuggestion.Add(new Knight(PlayerType.Npc, playerPosition, unitStats));
                        break;
                }
            }

            return spawnSuggestion;
        }

        //Hittade på nätet
        private List<E> ShuffleList<E>(List<E> inputList)
        {
            List<E> randomList = new List<E>();
            List<E> listToShuffle = new List<E>(inputList);

            Random r = new Random();
            int randomIndex = 0;
            while (listToShuffle.Count > 0)
            {
                randomIndex = r.Next(0, listToShuffle.Count); //Choose a random object in the list
                randomList.Add(listToShuffle[randomIndex]); //add it to the new, random list
                listToShuffle.RemoveAt(randomIndex); //remove to avoid duplicates
            }

            return randomList; //return the new random list
        }
    }
}
